﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Resources;
using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;


namespace CKS.FormsBasedAuthentication
{
    [Guid("c069b7b4-e2d4-438e-85c0-ff04f3688e66")]
    public class LoginWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        //References to controls
        private Login loginCtrl;
        private ResourceManager _resourceManager;

        #region Fields
        private string _UserNameLabelText = string.Empty;
        private string _CssClass = string.Empty;
        private string _DestinationPageUrl = string.Empty;
        private bool _DisplayRememberMe = false;
        private string _FailureText = string.Empty;
        private string _InstructionText = string.Empty;
        private string _LoginButtonmageUrl = string.Empty;
        private string _LoginButtonText = string.Empty;
        private ButtonType _LoginButtonType = ButtonType.Button;
        private string _PasswordLabelText = string.Empty;
        private string _PasswordRequiredErrorMessage = string.Empty;
        private string _RememberMeText = string.Empty;
        private string _TitleText = string.Empty;
        private string _UserNameRequiredErrorMessage = string.Empty;
        private bool _VisibleWhenLoggedIn = false;
        private string _CreateUserText = string.Empty;
        private string _CreateUserUrl = string.Empty;
        private string _CreateUserIconUrl = string.Empty;
        #endregion

        #region Constructors
        /// <summary>
        ///     Login Registration Constructor
        /// </summary>
        public LoginWebPart()
        {
            _resourceManager = new ResourceManager("CKS.FormsBasedAuthentication.LoginWebPart", Assembly.GetExecutingAssembly());
            try
            {
                UserNameLabelText = _resourceManager.GetString("UserNameLabelText_DefaultValue");
                PasswordLabelText = _resourceManager.GetString("PasswordLabelText_DefaultValue");
                CssClass = _resourceManager.GetString("CSSClass_DefaultValue");
                DestinationPageUrl = _resourceManager.GetString("DestinationPageUrl_DefaultValue");
                DisplayRememberMe = bool.Parse(_resourceManager.GetString("DisplayRememberMe_DefaultValue"));
                FailureText = _resourceManager.GetString("FailureText_DefaultValue");
                InstructionText = _resourceManager.GetString("InstructionText_DefaultValue");
                LoginButtonmageUrl = _resourceManager.GetString("LoginButtonImageUrl_DefaultValue");
                LoginButtonText = _resourceManager.GetString("LoginButtonText_DefaultValue");
                LoginButtonType = (ButtonType)Convert.ToInt32(_resourceManager.GetString("LoginButtonType_DefaultValue"));
                PasswordRequiredErrorMessage = _resourceManager.GetString("PasswordRequiredErrorMessage_DefaultValue");
                RememberMeText = _resourceManager.GetString("RememberMeText_DefaultValue");
                TitleText = _resourceManager.GetString("TitleText_DefaultValue");
                UserNameRequiredErrorMessage = _resourceManager.GetString("UserNameRequiredErrorMessage_DefaultValue");
                VisibleWhenLoggedIn = bool.Parse(_resourceManager.GetString("VisibleWhenLoggedIn_DefaultValue"));
                CreateUserIconUrl = _resourceManager.GetString("CreateUserIconUrl_DefaultValue");
                CreateUserText = _resourceManager.GetString("CreateUserText_DefaultValue");
                CreateUserUrl = _resourceManager.GetString("CreateUserUrl_DefaultValue");
            }
            catch (Exception ex)
            {
                Utils.LogError(ex);                
            }
        }
        #endregion

        #region Properties
        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("UserNameLabelText_FriendlyName", "UserNameLabelText_Category", "UserNameLabelText_Description")]
        public string UserNameLabelText
        {
            get { return _UserNameLabelText; }
            set { _UserNameLabelText = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("CSSClass_FriendlyName", "CSSClass_Category", "CSSClass_Description")]
        public new string CssClass
        {
            get { return _CssClass; }
            set { _CssClass = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("DestinationPageUrl_FriendlyName", "DestinationPageUrl_Category", "DestinationPageUrl_Description")]
        public string DestinationPageUrl
        {
            get { return _DestinationPageUrl; }
            set { _DestinationPageUrl = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("DisplayRememberMe_FriendlyName", "DisplayRememberMe_Category", "DisplayRememberMe_Description")]
        public bool DisplayRememberMe
        {
            get { return _DisplayRememberMe; }
            set { _DisplayRememberMe = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("FailureText_FriendlyName", "FailureText_Category", "FailureText_Description")]
        public string FailureText
        {
            get { return _FailureText; }
            set { _FailureText = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("InstructionText_FriendlyName", "InstructionText_Category", "InstructionText_Description")]
        public string InstructionText
        {
            get { return _InstructionText; }
            set { _InstructionText = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("LoginButtonImageUrl_FriendlyName", "LoginButtonImageUrl_Category", "LoginButtonImageUrl_Description")]
        public string LoginButtonmageUrl
        {
            get { return _LoginButtonmageUrl; }
            set { _LoginButtonmageUrl = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("LoginButtonText_FriendlyName", "LoginButtonText_Category", "LoginButtonText_Description")]
        public string LoginButtonText
        {
            get { return _LoginButtonText; }
            set { _LoginButtonText = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("LoginButtonType_FriendlyName", "LoginButtonType_Category", "LoginButtonType_Description")]
        public ButtonType LoginButtonType
        {
            get { return _LoginButtonType; }
            set { _LoginButtonType = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("PasswordLabelText_FriendlyName", "PasswordLabelText_Category", "PasswordeLabelText_Description")]
        public string PasswordLabelText
        {
            get { return _PasswordLabelText; }
            set { _PasswordLabelText = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("PasswordRequiredErrorMessage_FriendlyName", "PasswordRequiredErrorMessage_Category", "PasswordRequiredErrorMessage_Description")]
        public string PasswordRequiredErrorMessage
        {
            get { return _PasswordRequiredErrorMessage; }
            set { _PasswordRequiredErrorMessage = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("RememberMeText_FriendlyName", "RememberMeText_Category", "RememberMeText_Description")]
        public string RememberMeText
        {
            get { return _RememberMeText; }
            set { _RememberMeText = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("TitleText_FriendlyName", "TitleText_Category", "TitleText_Description")]
        public string TitleText
        {
            get { return _TitleText; }
            set { _TitleText = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("UserNameRequiredErrorMessage_FriendlyName", "UserNameRequiredErrorMessage_Category", "UserNameRequiredErrorMessage_Description")]
        public string UserNameRequiredErrorMessage
        {
            get { return _UserNameRequiredErrorMessage; }
            set { _UserNameRequiredErrorMessage = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("VisibleWhenLoggedIn_FriendlyName", "VisibleWhenLoggedIn_Category", "VisibleWhenLoggedIn_Description")]
        public bool VisibleWhenLoggedIn
        {
            get { return _VisibleWhenLoggedIn; }
            set { _VisibleWhenLoggedIn = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("CreateUserText_FriendlyName", "CreateUserText_Category", "CreateUserText_Description")]
        public string CreateUserText
        {
            get { return _CreateUserText; }
            set { _CreateUserText = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("CreateUserUrl_FriendlyName", "CreateUserUrl_Category", "CreateUserUrl_Description")]
        public string CreateUserUrl
        {
            get { return _CreateUserUrl; }
            set { _CreateUserUrl = value; }
        }

        [Personalizable(PersonalizationScope.Shared), WebBrowsable()]
        [ResourcesAttribute("CreateUserIconUrl_FriendlyName", "CreateUserIconUrl_Category", "CreateUserIconUrl_Description")]
        public string CreateUserIconUrl
        {
            get { return _CreateUserIconUrl; }
            set { _CreateUserIconUrl = value; }
        }

#endregion

        #region Methods
        /// <summary>
        ///     This method configures the LoginCtrl and then adds it to the DisplayPanel
        /// </summary>
        private void AddLoginCtrl()
        {
            loginCtrl = new Login();
            loginCtrl.UserNameLabelText = UserNameLabelText;
            loginCtrl.CssClass = CssClass;
            loginCtrl.DestinationPageUrl = DestinationPageUrl;
            loginCtrl.DisplayRememberMe = DisplayRememberMe;
            loginCtrl.FailureText = FailureText;
            loginCtrl.InstructionText = InstructionText;
            loginCtrl.LoginButtonImageUrl = LoginButtonmageUrl;
            loginCtrl.LoginButtonText = LoginButtonText;
            loginCtrl.LoginButtonType = LoginButtonType;
            loginCtrl.PasswordLabelText = PasswordLabelText;
            loginCtrl.PasswordRequiredErrorMessage = PasswordRequiredErrorMessage;
            loginCtrl.RememberMeText = RememberMeText;
            loginCtrl.TitleText = TitleText;
            loginCtrl.UserNameRequiredErrorMessage = UserNameRequiredErrorMessage;
            loginCtrl.VisibleWhenLoggedIn = VisibleWhenLoggedIn;
            loginCtrl.CreateUserIconUrl = CreateUserIconUrl;
            loginCtrl.CreateUserText = CreateUserText;
            loginCtrl.CreateUserUrl = CreateUserUrl;
            Controls.Add(loginCtrl);
        }

        /// <summary>
        ///     This is the standard method for adding controls to a web part.
        /// </summary>
        protected override void CreateChildControls()
        {
            AddLoginCtrl();
        }

        /// <summary>
        ///     Render the web part.
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            EnsureChildControls();
            base.RenderContents(writer);
        }

        public override string LoadResource(string id)
        {
            return (_resourceManager.GetString(id));
        }
        #endregion
    }
}
